<?php /** @var User $CurrentUser */ ?>
<div id="want-to-sell-dialog" title="发布商品">
    <form id="want-to-sell-form">
        <input type="hidden" name="user_id" value="<?php echo isset($CurrentUser) ? $CurrentUser->getId() : 0; ?>"/>

        <div class="left-right-side">
            <div class="input-group left-side">
                <label for="release-type" class="input-group-addon">发布标题</label>
                <input type="text" name="commodity_title" id="commodity-title" class="form-control" maxlength="20" placeholder="请填写商品名称" required>
            </div>
            <div class="input-group right-side">
                <label for="release-type" class="input-group-addon">发布类型</label>
                <select id="release-type" name="release_type" class="form-control">
                    <option value="2">二手</option>
                    <option value="1">全新</option>
                    <option value="3">赠送</option>
                </select>
            </div>
        </div>
        <div class="left-right-side">
            <div class="input-group left-side">
                <label for="commodity-category" class="input-group-addon">物品分类</label>
                <select id="commodity-category" name="commodity_category" class="form-control" style="float:left;width: 32%;">
                    <?php if (isset($MasterCommodityCategoryArray) && sizeof($MasterCommodityCategoryArray) > 0) : ?>
                        <?php foreach ($MasterCommodityCategoryArray as $index => $thisCommodityCategory) : ?>
                            <?php /** @var CommodityCategory $thisCommodityCategory */ ?>
                            <option id="commodity-category-<?php echo $thisCommodityCategory->getId(); ?>" value="<?php echo $thisCommodityCategory->getId(); ?>">
                                <?php echo $thisCommodityCategory->getName(); ?>
                            </option>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </select>

                <select id="commodity-sub-category" name="commodity_sub_category" class="form-control" style="float:left; width:68%;">
                    <?php if (isset($NotMasterCommodityCategoryArray) && sizeof($NotMasterCommodityCategoryArray) > 0) : ?>
                        <?php foreach ($NotMasterCommodityCategoryArray as $index => $thisCommodityCategory) : ?>
                            <?php /** @var CommodityCategory $thisCommodityCategory */ ?>
                            <option class="commodity-sub-category-option" value="<?php echo $thisCommodityCategory->getId(); ?>" data-father-id="<?php echo $thisCommodityCategory->getFatherId() ?>" style="display: none">
                                <?php echo $thisCommodityCategory->getName(); ?>
                            </option>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </select>
            </div>
            <div class="input-group right-side">
                <label for="commodity-price" class="input-group-addon">物品价格</label>
                <input type="number" id="commodity-price" name="commodity_price" class="form-control" min="0" max="10000000" placeholder="请输入售价" required/>
            </div>
        </div>
        <div class="left-right-side">
            <div class="input-group left-side">
                <label for="commodity-contact-info" class="input-group-addon">联系方式</label>
                <select id="commodity-contact-type" name="commodity_contact_type" class="form-control" style="float:left; width:32%">
                    <option value="<?php echo _ContactType_LongPhone ?>" <?php echo (($CurrentUser->getUserInfo()->getDefaultContactType())==_ContactType_LongPhone)?"selected":""; ?> >长号</option>
                    <option value="<?php echo _ContactType_ShortPhone ?>" <?php echo (($CurrentUser->getUserInfo()->getDefaultContactType())==_ContactType_ShortPhone)?"selected":""; ?>>短号</option>
                    <option value="<?php echo _ContactType_Qq ?>" <?php echo (($CurrentUser->getUserInfo()->getDefaultContactType())==_ContactType_Qq)?"selected":""; ?>>QQ</option>
                </select>
                <input type="text" id="commodity-contact-info" name="commodity_contact_info" class="form-control" style="float:left; width:68%" value="<?php echo (($CurrentUser->getUserInfo()->getDefaultContactType())==_ContactType_LongPhone)?$CurrentUser->getUserInfo()->getPhone():(((($CurrentUser->getUserInfo()->getDefaultContactType())==_ContactType_ShortPhone))?$CurrentUser->getUserInfo()->getPhoneShort():$CurrentUser->getUserInfo()->getQq());?>"/>
            </div>
            <div class="input-group right-side">
                <label for="commodity-stock" class="input-group-addon">物品库存</label>
                <input type="number" id="commodity-stock" name="commodity_stock" class="form-control" min="0" max="10000" placeholder="请输入库存" value="1" required/>
            </div>
        </div>
        <div class="input-group" style="position:relative;">
            <label for="commodity-intro" class="input-group-addon">物品简介</label>
            <input type="text" id="commodity-intro" name="commodity_intro" class="form-control commodity_intro" placeholder="在物品描述编写详细的物品相关信息" required/>
            <div class="limit-words">还可以输入 <b class="limit-words-num">140</b> 字</div>
        </div>
        <div class="input-group-has-textarea">
            <label for="commodity-content" class="input-group-addon">物品描述
                <small> - 推荐多上传实物图片，更受关注哦</small>
            </label>
            <textarea id="commodity-content" name="commodity_content" class="" rows="4" style="min-height:300px !important;"></textarea>
        </div>
        <div class="input-group">
            <input type="submit" class="form-control" value="发布"/>
        </div>
    </form>

    <script>
        var ActiveEditor;
        var TheFieldName;
        $(function () {
            var $commodityCategory = $('#commodity-category');
            $commodityCategory.val($commodityCategory.children().first().val());
            var $subCatSelect = $('#commodity-sub-category');

            function showSubCatByFatherId(fatherId) {
                var $nowToShowSubCat = $('option[data-father-id=' + fatherId + ']');
                $subCatSelect.children().hide();
                $nowToShowSubCat.show();
                $subCatSelect.val($nowToShowSubCat.first().val());
            }

            showSubCatByFatherId($commodityCategory.children().first().val());

            $commodityCategory.change(function () {
                var nowMasterCatId = $(this).val();
                $subCatSelect.children().hide();
                var $nowToShowSubCat = $('option[data-father-id=' + nowMasterCatId + ']');
                if ($nowToShowSubCat.length) {
                    $nowToShowSubCat.show();
                    $subCatSelect.show();
                    $commodityCategory.css('width', '32%');
                } else {
                    $subCatSelect.hide();
                    $commodityCategory.css('width', '100%');
                }
                $subCatSelect.val($nowToShowSubCat.first().val());
            });
            $("#want-to-sell-form").validate({
                submitHandler: function (form) {
                    $.ajax({
                        url: '<?php echo base_url('api/wantToSell');?>',
                        type: 'post',
                        data: $(form).serialize(),
                        success: function (jsonrsp) {
                            try {
                                var rsp = $.parseJSON(jsonrsp);
                                noty({type: rsp.type, text: rsp.message, callback: {
                                    afterClose: function () {
                                        switch (rsp.type) {
                                            case 'success':
                                                location.reload();
                                                break;
                                            default:
                                                break;
                                        }
                                    }
                                }});
                            } catch (ex) {
                                noty({type: 'error', text: '发生内部错误，请联系管理员'});
                                throw ex;
                            }
                        }
                    });
                }
            });
            UE.getEditor('commodity-content');

            $("#release-type").change(function () {
                if ($("#release-type").val() == "3") {
                    $('#commodity-price').val("0");
                    $('#commodity-price').attr("readonly", "readonly");
                } else {
                    $('#commodity-price').removeAttr("readonly");
                    $('#commodity-price').val("");
                }
            });

            $('#commodity-contact-type').change(function(){
                    switch ($(this).val()){
                        case '<?php echo _ContactType_LongPhone?>':
                            $('#commodity-contact-info').val('<?php echo $CurrentUser->getUserInfo()->getPhone()?>');
                            break;
                        case '<?php echo _ContactType_ShortPhone?>':
                            $('#commodity-contact-info').val('<?php echo $CurrentUser->getUserInfo()->getPhoneShort()?>');
                            break;
                        case '<?php echo _ContactType_Qq?>':
                            $('#commodity-contact-info').val('<?php echo $CurrentUser->getUserInfo()->getQq()?>');
                            break;
                    }
                }
            );
        });
    </script>
</div>
<!--限制字数-->
<script type="text/javascript"> 
   
   var txtobj = {
        divName     : "input-group", //外层容器的class
        textareaName: "commodity_intro", //textarea的class
        numName     : "limit-words-num", //数字的class
        num         : 140 //数字的最大数目
    }
    var textareaFn = function () {
        //定义变量
        var $onthis;//指向当前
        var $divname = txtobj.divName; //外层容器的class
        var $textareaName = txtobj.textareaName; //textarea的class
        var $numName = txtobj.numName; //数字的class
        var $num = txtobj.num; //数字的最大数目
        function isChinese(str) {  //判断是不是中文
            var reCh = /[u00-uff]/;
            return !reCh.test(str);
        }

        function numChange() {
            var strlen = 0; //初始定义长度为0
            var txtval = $.trim($onthis.val());
            for (var i = 0; i < txtval.length; i++) {
                if (isChinese(txtval.charAt(i)) == true) {
                    strlen = strlen + 2;//中文为2个字符
                } else {
                    strlen = strlen + 1;//英文一个字符
                }
            }
            strlen = Math.ceil(strlen / 2);//中英文相加除2取整数
            if ($num - strlen < 0) {
                $par.html("超出 <b style='color:red;font-weight:lighter' id='chaochu' class=" + $numName + ">" + Math.abs($num - strlen) + "</b> 字"); //超出的样式
            }
            else {
                $par.html("还可以输入 <b class=" + $numName + ">" + ($num - strlen) + "</b> 字"); //正常时候
            }
            $b.html($num - strlen);
        }

        $("." + $textareaName).on("focus", function () {
            $b = $(this).parents("." + $divname).find("." + $numName); //获取当前的数字
            $par = $b.parent();
            $onthis = $(this); //获取当前的textarea
            var setNum = setInterval(numChange, 500);
        });
    }
    textareaFn();
</script>
